package com.example.springboottestgit.service;

import com.example.springboottestgit.bean.BusinessException;
import com.example.springboottestgit.bean.InviteTenderContentQaRecord;
import com.example.springboottestgit.bean.InviteTenderFileUploadRecord;
import com.example.springboottestgit.bean.vo.InviteTenderFileVO;
import com.example.springboottestgit.bean.vo.TenderQuestionVO;
import com.example.springboottestgit.dao.InviteTenderContentQaRecordMapper;
import com.example.springboottestgit.dao.InviteTenderFileUploadRecordMapper;
import com.example.springboottestgit.dao.TenderQuestionTemplateMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @description:招标文件-法律法规
 * @author: ZZZ
 */
@Slf4j
@Service
public class InviteTenderService {

    @Value("${tender.analysis.path}")
    private String tenderAnalysisPath;

    @Autowired
    private TenderQuestionTemplateMapper tenderQuestionTemplateMapper;

    @Autowired
    private InviteTenderContentQaRecordMapper inviteTenderContentQaRecordMapper;

    @Autowired
    private InviteTenderFileUploadRecordMapper inviteTenderFileUploadRecordMapper;

    @Autowired
    private ModelServer modelServer;

    //@Scheduled(cron = "0/30 * * * * ? ")
    public void inviteTask() {
        List<InviteTenderFileUploadRecord> list = inviteTenderFileUploadRecordMapper.queryFileStatusList();
        for (InviteTenderFileUploadRecord vo : list) {
            Integer questionNum = vo.getQuestionNum();
            int count = inviteTenderFileUploadRecordMapper.questionNum(vo.getId());
            if (questionNum == count) {
                InviteTenderFileUploadRecord update = new InviteTenderFileUploadRecord();
                update.setId(vo.getId());
                update.setStatus(1);
                inviteTenderFileUploadRecordMapper.updateById(update);
                log.info(vo.getFileName() + "更新文件状态");
            }
        }
    }


    public InviteTenderFileVO inviteTenderDetailQuery(long id) {
        InviteTenderFileVO vo = new InviteTenderFileVO();
        InviteTenderFileUploadRecord inviteTenderFileUploadRecord = inviteTenderFileUploadRecordMapper.selectById(id);
        if (inviteTenderFileUploadRecord != null) {
            BeanUtils.copyProperties(inviteTenderFileUploadRecord, vo);
        }
        return vo;
    }


    public void inviteTenderContentQa(String newFileName, long id) {
        InviteTenderFileUploadRecord inviteTenderFileUploadRecord = new InviteTenderFileUploadRecord();
        inviteTenderFileUploadRecord.setId(id);
        try {
            LocalDateTime s = LocalDateTime.now();
            String filePath = tenderAnalysisPath + newFileName;
            log.info("========招标文件内容问答==========");
            List<TenderQuestionVO> list = tenderQuestionTemplateMapper.queryQuestion();
            inviteTenderFileUploadRecord.setQuestionNum(list.size());
            inviteTenderFileUploadRecordMapper.updateById(inviteTenderFileUploadRecord);

            for (TenderQuestionVO tenderQuestionVO : list) {
                long questionId = tenderQuestionVO.getQuestionId();
                String questionName = tenderQuestionVO.getInviteQuestionName();

                InviteTenderContentQaRecord insert = new InviteTenderContentQaRecord();
                insert.setInviteTenderFileId(id);
                insert.setQuestionId(questionId);
                insert.setQuestionName(questionName);
                //String result = questionRetrieval(questionName, filePath);
                //String result = "hello world";
                //insert.setResult(result);
                //inviteTenderContentQaRecordMapper.insert(insert);
                modelServer.inviteTenderContentQa(insert, questionName, filePath);
            }

            //inviteTenderFileUploadRecord.setStatus(1);

            LocalDateTime e = LocalDateTime.now();
            Duration between = Duration.between(s, e);
            log.info("招标文件:{}  内容问答耗时 {} 秒", newFileName, between.getSeconds());
        } catch (Exception e) {
            //失败
            inviteTenderFileUploadRecord.setStatus(2);
            inviteTenderFileUploadRecordMapper.updateById(inviteTenderFileUploadRecord);
            throw new BusinessException("001001", "招标文件解析失败");
        }

    }

}
